# -*- coding: utf-8 -*-

import MySQLdb
import urllib2
from xml.dom import minidom
from util import *

API_KEY="09ebbaa767412e021dc32ff635c38a06"


def get_user_list():
    "Get all new users from [new_users] table"
    db = None
    try:
        db = opendb()
        cursor = db.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute("select id, user_id from new_users order by id asc limit 2000")
        return cursor.fetchall()

    finally:
        if db:
            db.close()



all_users = get_user_list()

db = opendb()

cursor = db.cursor()

for user in all_users:
   
    doc = urllib2.urlopen("http://api.douban.com/people/%s?apikey=%s" % (user["user_id"],API_KEY) ).read()

    print doc

    if not doc:
        print "fetch %s error." % user["user_id"]
        continue


    user = {}

    dom = minidom.parseString(doc)

    id =  dom.getElementsByTagName("id")[0].firstChild.nodeValue
    user["id"] = id.split("/")[-1]
    title = dom.getElementsByTagName("title")[0].firstChild.nodeValue
    user["title"] = title

    title = dom.getElementsByTagName("title")[0].firstChild.nodeValue
    uid = dom.getElementsByTagName("db:uid")[0].firstChild.nodeValue
    user['uid'] = uid


    if len(dom.getElementsByTagName("db:location")) > 0:
        node = dom.getElementsByTagName("db:location")[0]
        user["loc"] = node.firstChild.nodeValue
        user["loc_id"] = node.getAttribute("id")
    else:
        user['loc']=''
        user['loc_id']=''

    """TODO:escape sign,content"""

    if len(dom.getElementsByTagName("db:signature")) > 0:
        node = dom.getElementsByTagName("db:signature")[0]
        user["sign"] = node.firstChild.nodeValue
    else:
        user['sign']=''

    if len(dom.getElementsByTagName("content")) > 0:
        node = dom.getElementsByTagName("content")[0]
        user["content"] = node.firstChild.nodeValue
    else:
        user['content'] =''


    for node in dom.getElementsByTagName("link"):
        if node.getAttribute("rel") == 'icon':
            user["icon"] = node.getAttribute("href")
            break

    """use named parameters in mysqldb"""
    cursor.execute( "insert into user_profiles (iuid, uid, title, loc, loc_id, sign, content, icon) values (%s, %s, %s, %s, %s, %s, %s, %s)" , (user["id"], user['uid'], escape_string(user['title']), user['loc'], user['loc_id'], escape_string(user['sign']), escape_string(user['content']), user['icon']))
    db.commit()
"""
    cursor.execute("select content from user_profiles")
    user=cursor.fetchone()
    print user[0]
"""

